<?php

namespace plugin\admin\app\model;

use plugin\admin\app\model\Base;
use support\Cache;

class NetworkOrder extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_network_order';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    protected $guarded = [];

    protected $dateFormat = 'U';

    protected $appends = ['order_source_text','pay_method_text'];

    public function getOrderSourceTextAttribute(){
        $value = $this->attributes['orderSource'] ?? '';
        if (!empty($value)){
            switch ($value){
                case 'MEITUAN_WAIMAI':
                    return '美团外卖';
                case 'ELEME_WAIMAI':
                    return '饿了么';
                case 'ELEBE_WAIMAI':
                    return '饿百';
                case 'ZIYING_MINIAPP':
                    return '银豹小程序';
                case 'ZIYING_MIAOSHA':
                    return '秒杀单';
                case 'ZIYING_KANJIA':
                    return '砍价';
                default:
                    return '其他';
            }
        }
        return '未知';
    }

    public function getPayMethodTextAttribute(){
        $value = $this->attributes['payMethod'] ?? '';
        if (!empty($value)){
            switch ($value){
                case 'CustomerBalance':
                    return '余额支付';
                case 'Cash':
                    return '现金支付';
                case 'pospalopenpay_aggregation.银豹付微信小程序支付.miniapp':
                    return '银豹付';
                case 'ZIYING_MINIAPP':
                    return '银豹小程序';
                case 'ZIYING_MIAOSHA':
                    return '秒杀单';
                case 'ZIYING_KANJIA':
                    return '砍价';
                default:
                    $name = Cache::get('pay_method_'.$value);
                    if (!empty($name)){
                        return $name;
                    }
                    $name =PayMethodModel::where('code',$value)->value('showName');
                    if (!empty($name)){
                        Cache::set('pay_method_'.$value,$name,3600);
                        return $name;
                    }
                    return '其他';
            }
        }
        return '未知';
    }
    protected function getItemsAttribute($value)
    {
        if (!empty($value)){
            return json_decode($value, true);
        }
        return [];
    }

    
}
